package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.PjPendingBean;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.newProjectCenter.mapper.PjPendingMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.Date;

/**
 * 工程挂起
 */
public class PjPendingInsert implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            PjPendingBean pjPending = JsonUtils.objectToPojo(req.getData(), PjPendingBean.class);
            if (StringUtils.isBlank(pjPending.getPjId())){
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "id 不能为空", Void.class);
            }
            if (StringUtils.isBlank(pjPending.getPendingReason())){
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "挂起原因不能为空", Void.class);
            }

            PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);
            PjInfoBean pjInfo = pjInfoMapper.selectById(pjPending.getPjId());
            pjInfo.setPendingState(1); // 挂起中
            pjInfoMapper.update(pjInfo);

            PjPendingMapper pjPendingMapper = factory.getMapper(PjPendingMapper.class);
            pjPending.setCreateInfo(userInfo);
            pjPending.setBillDate(new Date());
            pjPending.setPendingState(1);
            pjPending.setPjProcessState(pjInfo.getProcessState());
            pjPending.setPjNodeName(pjInfo.getProcessName());
            pjPending.setBillNo(Tools.getBillNo(factory, "pj_pending"));
            pjPending.setPjNo(pjInfo.getBillNo());
            pjPendingMapper.addPjPending(pjPending);

            MessageBean<String> messageBean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS,"执行成功",String.class);
            messageBean.setData(pjPending.getId());
            return messageBean;
        } catch (Exception e) {
            logger.error("工程挂起新增失败",e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL,"工程挂起新增失败",null);
        }
    }
}
